﻿using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.WebControls;
using System;
using System.Collections;
using System.Configuration;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class InstrumentCommissionExtra : BasePage
    {
        public Guid MaintainID
        {
            get
            {
                if (hiddenMaintainID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenMaintainID.Value);
            }
            set
            {
                hiddenMaintainID.Value = value.ToString();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["maintainID"] != null)
                {
                    MaintainID = new Guid(Request.QueryString["maintainID"]);
                    WsmMaintainBillEntity maintainEntity = new SystemBll().Get<WsmMaintainBillEntity>(MaintainID);
                    WsmTaskRegisterEntity registerEntity = new SystemBll().Get<WsmTaskRegisterEntity>(maintainEntity.RegisterID);
                    WsmInstrumentInfoEntity instrumentEntity = new SystemBll().Get<WsmInstrumentInfoEntity>(registerEntity.InstrumentID);

                    List<WsmInstrumentCommissionEntity> commissionEntitys = new SystemBll().GetAll<WsmInstrumentCommissionEntity>("and MaintainID='" + MaintainID.ToString() + "'");
                    if (commissionEntitys != null && commissionEntitys.Count > 0)
                    {
                        WsmInstrumentCommissionEntity entity = commissionEntitys[0];
                        Calibration.SelectedValue = entity.Calibration.ToString();
                        CalibrationOther.Text = entity.CalibrationOther;
                        QualityControlOther.Text = entity.QualityControlOther;
                        QualityControl.SelectedValue = entity.QualityControl.ToString();
                        txtProjContent.Text = entity.ProjContent;
                        txtServiceProj.Text = entity.ServiceProj;
                        txtMemo.Text = entity.Memo;
                        ProjNumber.Value = entity.ProjNumber;
                        ServiceProjNumber.Value = entity.ServiceProjNumber;
                        txtInstrumentName.Text = entity.InstrumentName;
                    }
                }
            }
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            string Message = "";
            if (!isValidate(ref Message))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "saveansrror", "alert('" + Message + "');", true);
                return;
            }

            SystemBll bll = new SystemBll();
            bll.Delete<WsmInstrumentCommissionEntity>(" and MaintainID='" + MaintainID.ToString() + "'");

            WsmInstrumentCommissionEntity entity = new WsmInstrumentCommissionEntity();
            entity.Calibration = System.Convert.ToInt32(Calibration.SelectedValue);
            entity.CalibrationOther = CalibrationOther.Text.Trim();
            entity.QualityControlOther = QualityControlOther.Text.Trim();
            entity.QualityControl = System.Convert.ToInt32(QualityControl.SelectedValue);
            entity.ProjContent = txtProjContent.Text.Trim();
            entity.ServiceProj = txtServiceProj.Text.Trim();
            entity.ProjNumber = Convert.ToInt16(ProjNumber.Value);
            entity.ServiceProjNumber = Convert.ToInt16(ServiceProjNumber.Value);
            entity.Memo = txtMemo.Text.Trim();
            entity.CommissionID = Guid.NewGuid();
            entity.MaintainID = MaintainID;
            entity.InstrumentName = txtInstrumentName.Text;
            bll.Insert(entity);
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');", true);
        }


        private bool isValidate(ref string message)
        {
            bool isValidate = true;
            if (this.txtInstrumentName.Text.Trim() == "")
            {
                message = "请指定仪器名称";
                isValidate = false; return isValidate;
            }
            if (txtProjContent.Text.Trim() == "")
            {
                message = "请输入本系统开展项目内容";
                isValidate = false; return isValidate;
            }
            if (ProjNumber.Value == null || ProjNumber.Value==0)
            {
                message = "请输入本系统开展项目总数";
                isValidate = false; return isValidate;
            }
            if (txtServiceProj.Text.Trim()=="")
            {
                message = "请输入本系统服务项目内容";
                isValidate = false; return isValidate;
            }
            if (ServiceProjNumber.Value == null || ServiceProjNumber.Value == 0)
            {
                message = "请输入本系统服务项目总数";
                isValidate = false; return isValidate;
            }
            if (txtMemo.Text.Trim()=="")
            {
                message = "请输入本系统项目变更情况";
                isValidate = false; return isValidate;
            }
            
            if (message != "")
            {
                isValidate = false;
            }
            return isValidate;
        }
    }
}
